//
//  LoginViewController.m
//  豆瓣_精简版
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import "DataBase.h"
@interface LoginViewController ()<RootDelegate>

@property(nonatomic,retain)LoginView *loginV;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登陆";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];
    
    self.loginV = [[LoginView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.loginV];
    self.loginV.myDelegate = self;
    
}

//登陆
-(void)delegateLogin{
    
    if (self.loginV.nameTF.text.length == 0) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户名" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:action];
        
    }else{
        NSMutableArray *array = [[DataBase shareDataBase]selectUserData:self.loginV.nameTF.text];
        
        if ([self.loginV.passwordTF.text isEqualToString:[[array objectAtIndex:0]password]]) {
            [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"state"];
            [[NSUserDefaults standardUserDefaults]setObject:self.loginV.nameTF.text forKey:@"user"];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else{
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不对" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            [alert addAction:action];
            
        }
    }
    
}


//注册
-(void)delegateRegister{
    
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}


//返回按钮
- (void)leftAction:(UIBarButtonItem *)bar{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
